﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

/*********************************************************
 * 命名空间 Simple.Tools.Convert.tree
 * 接口名称 SimpleTree
 * 开发人员：ADMIN
 * 创建时间：2024/1/5 16:24:29
 * 描述说明：
 * 更改历史：
 * 
 * *******************************************************/
namespace Simple.Tools
{
	/// <summary>
	/// 
	/// </summary>
	/// <typeparam name="TData"></typeparam>
	/// <typeparam name="TKey"></typeparam>
	public class SimpleTree<TData,TKey> : ITree<TData,TKey>,IEquatable<TKey>
	{
		/// <summary>
		/// 
		/// </summary>
		public TKey Id { get { return key; } set { key = value; } }
		/// <summary>
		/// 
		/// </summary>
		public TKey ParentId { get { return parentId; } set { parentId = value; } }
		/// <summary>
		/// 
		/// </summary>
		public List<TData> Children { get { return children; } set { children = value; } }

		private TKey key;
		private TKey parentId;
		private List<TData> children;
		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		public virtual bool  IsRoot()
		{
			Type type = typeof(TKey);
			if (type == typeof(int))
			{
				int value = -1;
				if (int.TryParse(key.ToString(), out value))
				{
					return value == 0;
				}
			}
			else if (type == typeof(string))
			{
				
				return SimpleCheck.IsNullOrEmpty(key.ToString());
            }
			else if (type == typeof(Guid))
			{
				Guid value = Guid.NewGuid();
				if (Guid.TryParse(key.ToString(), out value))
				{
					return value.IsEmpty();
				}
			}
			return false;
		}
		/// <summary>
		/// 
		/// </summary>
		/// <param name="other"></param>
		/// <returns></returns>
		public virtual bool Equals(TKey other)
		{
			if (SimpleCheck.IsNull(other))
			{
				return false;
			}
			return other.Equals(this.key);
		}
	}
}
